package com.zhanglifan.action;

import com.opensymphony.xwork2.ModelDriven;
import com.zhanglifan.domain.Module;
import com.zhanglifan.domain.Role;
import com.zhanglifan.exception.CustomizeException;
import com.zhanglifan.service.ModuleService;
import com.zhanglifan.service.RoleService;
import com.zhanglifan.util.Page;
import com.zhanglifan.util.UtilFuns;

import java.util.HashSet;
import java.util.Set;

/**
 * Information
 * Author: ZhangLifan
 * Time: 2017/9/24 20:09
 * Description: RoleAction
 */
public class RoleAction extends BaseAction implements ModelDriven<Role> {
    private RoleService roleService;
    private ModuleService moduleService;

    private String moduleIds;

    private Role role = new Role();

    private Page<Role> page = new Page<>();

    public String list(){
        roleService.findPage("from Role order by orderNo",page,Role.class,null);
        page.setUrl("roleAction_list");
        super.push(page);
        return "listSuccess";
    }

    public String toview(){
        Role role = roleService.get(Role.class, this.role.getId());
        super.push(role);
        return "toViewSuccess";
    }

    public String tocreate(){
        return "toCreateSuccess";
    }

    public String insert(){
        roleService.saveOrUpdate(role);
        return "insertSuccess";
    }

    public String toupdate(){
        if (UtilFuns.isEmpty(role.getId())){
            throw new CustomizeException("ID不能为空,请选择一个角色");
        }

        Role role = roleService.get(Role.class, this.role.getId());
        super.push(role);
        return "toUpdateSuccess";
    }

    public String update(){

        Role r = roleService.get(Role.class,role.getId());
        r.setName(role.getName());
        r.setRemark(role.getRemark());
        roleService.saveOrUpdate(r);
        return "updateSuccess";
    }

    public String delete(){
        Role r = roleService.get(Role.class,role.getId());
        moduleService.removeFromRedis(r);
        System.out.println("*** redis_delete ***");
        roleService.deleteById(Role.class,role.getId());
        return "deleteSuccess";
    }

    public String tomodule(){

        Role role = roleService.get(Role.class, this.role.getId());
        this.role.setName(role.getName());
        super.put("zNodes",moduleService.getFromRedis(role));

        return "toModuleSuccess";
    }

    public String module(){
        String[] ids = moduleIds.split(",");
        Set<Module> modules = new HashSet<>();
        Role role = roleService.get(Role.class, this.role.getId());
        moduleService.removeFromRedis(role);
        System.out.println("*** redis_delete ***");
        for (String id : ids) {
            modules.add(moduleService.get(Module.class,id));
        }

        role.setModules(modules);
        
        roleService.saveOrUpdate(role);

        return "moduleSuccess";
    }

    public void setModuleIds(String moduleIds) {
        this.moduleIds = moduleIds;
    }

    public Page<Role> getPage() {
        return page;
    }

    public void setRoleService(RoleService roleService) {
        this.roleService = roleService;
    }

    public void setModuleService(ModuleService moduleService) {
        this.moduleService = moduleService;
    }

    @Override
    public Role getModel() {
        return role;
    }
}
